import os
import threading
import time
import shutil

from tqdm import tqdm

input_paths = [r"H:\DCIM\101CANON", r"H:\DCIM\100CANON"]
output_path = r"G:\图\RAWs"
# 得到文件列表
file_list = []
for input_path in input_paths:
    if os.path.exists(input_path):
        files = os.listdir(input_path)
        for file in files:
            file_path = os.path.join(input_path, file)
            file_list.append(file_path)

print(len(file_list))
# 得到输出文件夹下所有的文件名称
# output_file_list = []
# for root, dirs, files in os.walk(output_path):
#     for file in files:
#         output_file_list.append(os.path.join(root, file))
# 提取文件名
# file_name_list = [i.split("\\")[-1] for i in output_file_list]
# file_name_set = set(file_name_list)

tqd = tqdm(total=len(file_list))


thread_num = 5
thread_list = []
sem = threading.Semaphore(thread_num)

mkdir_lock = threading.Lock()


def update_tqd(file, dt, output_dir_path):
    tqd.update(1)
    tqd.set_description("%s %s %s" % (file, dt, output_dir_path))


def processer(file):
    # file_path = os.path.join(input_path, file)
    ctime = os.path.getctime(file)
    # 转换成时间
    time_local = time.localtime(ctime)
    # 转换成新的时间格式(2016-05-05 20:28:54)
    dt = time.strftime("%Y/%Y-%m-%d", time_local)
    # 按日分类，创建文件夹，格式：2019_01_01
    output_dir_path = os.path.join(output_path, dt)
    with mkdir_lock:
        if not os.path.exists(output_dir_path):
            os.makedirs(output_dir_path)

    file_name = file.split('\\')[-1]
    out_file_path = os.path.join(output_dir_path, file_name)
    if os.path.exists(out_file_path):
        # print(out_file_path, '已存在')
        pass
    else:
        with sem:
            # 复制文件
            shutil.copy(file, output_dir_path)
            # print(file, dt, output_dir_path, "移动")
    update_tqd(file, dt, output_dir_path)


# 循环，提取修改日期
for file in file_list:
    # if file in file_name_set:
    #     print(file, "已存在")
    #     continue
    thread_list.append(threading.Thread(target=processer, args=(file,)))
    # processer(file)

for i in thread_list:
    while True:
        try:
            i.start()
            break
        except:
            print("error, retry")
            time.sleep(0.5)

for i in thread_list:
    while True:
        try:
            i.join()
            break
        except:
            print("error, retry")
            time.sleep(0.5)
